package Controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Model.Bean.Phone;
import Model.Util.AdminModel;

/**
 * Servlet implementation class AddPhoneAction
 */
@WebServlet("/AddPhoneAction")
public class AddPhoneAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddPhoneAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String phoneName = request.getParameter("PhoneName");
		int phoneType = Integer.parseInt(request.getParameter("PhoneType"));
		String description = request.getParameter("Description");
		double price = Double.parseDouble(request.getParameter("Price"));
		String picture = request.getParameter("Picture");
		
		AdminModel model = new AdminModel();
		
		Phone phone = new Phone();
		phone.setPhoneName(phoneName);
		phone.setPhoneType(phoneType);
		phone.setDescription(description);
		phone.setPrice(price);
		phone.setPicture(picture);
		
		if(model.AddPhone(phone)) {
			response.sendRedirect("frmAddPhoneSuccess");
		}
		else {
			request.setAttribute("isAddPhoneFail", true);
			request.getRequestDispatcher("frmAddPhone.jsp").forward(request, response);
		}
	}

}
